package com.example.restatr.Atcivity.LawyerConsultation;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Bean.CardBean;
import com.example.restatr.Atcivity.LawyerConsultation.Bean.LawyerInfoBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class LawyerListAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ListView lv;
    private CardBean data;
    private List<LawyerInfoBean.RowsBean> rowsBeans = new ArrayList<>();
    private LinearLayout loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_lawyer_list_at );

        toolbar = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        lv = findViewById( R.id.lv );
        loading = findViewById( R.id.loading );

        data = new Gson().fromJson( getIntent().getStringExtra( "data" ), new TypeToken<CardBean>() {
        }.getType() );
        Tools.SetBarTitle( toolbar, barTitle, "律师列表", search, this, false );
        GetLawyerInfo( "/prod-api/api/lawyer-consultation/lawyer/list?sort=favorableRate&legalExpertiseId=" + data.getId() );
    }

    private void GetLawyerInfo(String link) {
        new Thread( () -> {
            String token = Tools.Get( link, MainActivity.sp.getString( "token", "" ) );
            LawyerInfoBean lawyerInfoBean = new Gson().fromJson( token, LawyerInfoBean.class );
            rowsBeans.addAll( lawyerInfoBean.getRows() );
            runOnUiThread( () -> {
                if (rowsBeans.size() > 0) {
                    loading.setVisibility( View.GONE );
                    lv.setVisibility( View.VISIBLE );
                    lv.setAdapter( new adapter() );
                }
            } );
        } ).start();
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return rowsBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return rowsBeans.get( position );
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate( R.layout.lawyer_item, null );
            ImageView head = convertView.findViewById( R.id.head );
            TextView name = convertView.findViewById( R.id.name );
            TextView workStartAt = convertView.findViewById( R.id.workStartAt );
            TextView serviceTimes = convertView.findViewById( R.id.serviceTimes );
            TextView favorableRate = convertView.findViewById( R.id.favorableRate );
            TextView legalExpertiseName = convertView.findViewById( R.id.legalExpertiseName );
            CardView cardBtn = convertView.findViewById( R.id.cardBtn );

            Glide.with( LawyerListAt.this )
                    .load( Tools.ip + rowsBeans.get( position ).getAvatarUrl() )
                    .transform( new CircleCrop() )
                    .into( head );
            name.setText( rowsBeans.get( position ).getName() );
            workStartAt.setText( "从业时间" + rowsBeans.get( position ).getWorkStartAt() );
            serviceTimes.setText( rowsBeans.get( position ).getServiceTimes() + "人咨询" );
            favorableRate.setText( "好评率" + rowsBeans.get( position ).getFavorableRate() + "%" );
            legalExpertiseName.setText( rowsBeans.get( position ).getLegalExpertiseName() );
            cardBtn.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent( LawyerListAt.this, LawyerInfoAt.class );
                    intent.putExtra( "data", new Gson().toJson( rowsBeans.get( position ) ) );
                    startActivity( intent );
                }
            } );
            return convertView;
        }
    }
}